<html>
    <head>
    <title>Table Sort Example</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
            
            //转换器，将列的字段类型转换为可以排序的类型：String,int,float
            function convert(sValue, sDataType) {
                switch(sDataType) {
                    case "int":
                        return parseInt(sValue);
                    case "float":
                        return parseFloat(sValue);
                    case "date":
                        return new Date(Date.parse(sValue));
					case "length":
						return sValue.length;
                    default:
                        return sValue.toString();
                
                }
            }
            
            //排序函数产生器，iCol表示列索引，sDataType表示该列的数据类型
            function generateCompareTRs(iCol, sDataType) {
        
                return  function compareTRs(oTR1, oTR2) {
                            var vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType);
                            var vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);
        
                            if (vValue1 < vValue2) {
                                return -1;
                            } else if (vValue1 > vValue2) {
                                return 1;
                            } else {
                                return 0;
                            }
                        };
            }
            
            //排序方法
            function sortTable(sTableID, iCol, sDataType) {
                var oTable = document.getElementById(sTableID);
                var oTBody = oTable.tBodies[0];
                var colDataRows = oTBody.rows;
                var aTRs = new Array;
                
                //将所有列放入数组
                for (var i=0; i < colDataRows.length; i++) {
                    aTRs[i] = colDataRows[i];
                }
                 
                //判断最后一次排序的列是否与现在要进行排序的列相同，是的话，直接使用reverse()逆序
                if (oTable.sortCol == iCol) {
                    aTRs.reverse();
                } else {
                    //使用数组的sort方法，传进排序函数
                    aTRs.sort(generateCompareTRs(iCol, sDataType));
                }
        
                var oFragment = document.createDocumentFragment();
                for (var i=0; i < aTRs.length; i++) {
                    oFragment.appendChild(aTRs[i]);
                }
       
                oTBody.appendChild(oFragment);
                //记录最后一次排序的列索引
                oTable.sortCol = iCol;
            }

        </script>
    </head>
    <body>

	<?php
	error_reporting(E_ALL|E_STRICT);
	set_time_limit(0);
	date_default_timezone_set('PRC');
	//ini_set('display_errors', false);

	require("DomainFactoryClass.php");

	if (!empty($_POST['domain'])) {
		$domain = $_POST['domain'];
		$decorates = empty($_POST['decorates'])?array():$_POST['decorates'];
		$domainSuffix = empty($_POST['domainsuffix'])?array():$_POST['domainsuffix'];
		$df = new DomainFactory($domain, $decorates, $domainSuffix, 1, 1);
		$domainResult = $df->factory();
		//var_dump($decorates); die;
	}

	
	//$df = new DomainFactory('', array(), array(), 1, 1);
	//$df->pinyinChar();
	//$df = new DomainFactory('liuphp.com', 'decorate', 1, 1);
	?>	


	<?php if (!empty($domainResult)) : ?>
	<table border="1" id="tblSort">
		<thead>
			<tr>
				<th onclick="sortTable('tblSort', 0, 'length')" style="cursor:pointer">域名</th>
				<th onclick="sortTable('tblSort', 1)" style="cursor:pointer">后缀</th>
				<th onclick="sortTable('tblSort', 2, 'date')" style="cursor:pointer">到期时间</th>
				<th onclick="sortTable('tblSort', 3, 'float')" style="cursor:pointer">运行时间</th>
			</tr>
		</thead>
		<tbody>
		<?php foreach ($domainResult as $k => $domain): ?>
			<?php 
			if ($k == 'totalTime') continue;
			foreach ($domain['suffix'] as $suffix => $result): ?>
			<tr>
				<td><?php echo $domain['domain']; ?></td>
				<td><?php echo $suffix; ?></td>
				<td><?php echo $result['value']; ?></td>
				<td><?php echo $result['runtime']; ?></td>
			</tr>
			<?php endforeach; ?>
		<?php endforeach; ?>
		</tbody>
	</table>
	<?php?><?php echo $domainResult['totalTime']; ?>        
	<?php endif; ?>
    <form action="" method="post">
    <table>
        <tr><th>域名：</th><td><input type="text" name="domain" /></td></tr>
        <tr>
            <th colspan="2">操作类型：</th>
        </tr>
        <tr><td><input type="checkbox" name="decorates[]" value="switchChar" />临近字符调换</td></tr>
        <tr><td><input type="checkbox" name="decorates[]" value="loseChar" />任意字符缺失</td></tr>
        <tr><td><input type="checkbox" name="decorates[]" value="doubleChar" />任意字符增加</td></tr>
		<tr><td><input type="checkbox" name="decorates[]" value="wwwPrefixChar" />www前缀</td>></tr>
		<tr><td><input type="checkbox" name="decorates[]" value="vPrefixChar" />v前缀</td></tr>
        <tr>
            <th colspan="2">域名后缀：</th>
        </tr>
		<tr>
			<th>.com<input type="checkbox" name="domainsuffix[]" value=".com" /></th>
			<th>.cn<input type="checkbox" name="domainsuffix[]" value=".cn" /></th>
			<th>.net<input type="checkbox" name="domainsuffix[]" value=".net" /></th>
			<th>.com.cn<input type="checkbox" name="domainsuffix[]" value=".com.cn" /></th>
		</tr>
        <!--<tr><th>翘舌音平舌音：</th><th><input type="checkbox" name="decorates[]" value="value4" /></th></tr>-->
        <!--<tr><th>前鼻音后鼻音：</th><th><input type="checkbox" name="decorates[]" value="value5" /></th></tr>-->
        <tr><th colspan="2"><input type="submit" /></th></tr>
    </table>
    </form>   
</body>
</html>


<?php
/*
if(!empty($_GET['domain'])) {
    $domain = $_GET['domain'];
    $domainfront = explode('.', $domain);
    $length = strlen($domainfront[0]);

    $editstr = substr($domainfront[0], 1, ($length-2));
    $prefixstring = substr($domainfront[0], 0, 1);
    $suffixstring = substr($domainfront[0], strlen($domainfront[0])-1, 1); //die($suffixstring);

    $suffix = $domainfront[1];
    $chararr = str_split($editstr);
    echo "输入的域名：{$domain} | 改后域名：{$editstr} | 长度：{$length} | {$_GET['operation1']}";

    if($_GET['operation1'] == 'on') {
        echo "临近字符调换<br />";
        for($i=0;$i<(count($chararr)-1);$i++) {
            $chararrtmp = $chararr;
            $tmp = $chararrtmp[$i];
            $chararrtmp[$i] = $chararrtmp[$i+1];
            $chararrtmp[$i+1] = $tmp;
            $charfinal[] = "{$prefixstring}".implode('', $chararrtmp)."{$suffixstring}.{$suffix}";
        }
        
        //echo "<pre>";
        //var_dump($charfinal);
        //echo "</pre>";
        
    }

    if($_GET['operation2'] == 'on') {
        echo "任意字符缺失<br />";
        for($i=0;$i<(count($chararr)-1);$i++){
            $chararrtmp = $chararr;
            unset($chararrtmp[$i]);
            $charfinal[] = "{$prefixstring}".implode('', $chararrtmp)."{$suffixstring}.{$suffix}";
        }
        
        //echo "<pre>";
        //var_dump($charfinal);
        //echo "</pre>";
        
    }

    if($_GET['operation3'] == 'on') {
        echo "翘舌音平舌音<br />";
    }

    if($_GET['operation4'] == 'on') {
        echo "前鼻音后鼻音<br />";
    }

    if($_GET['operation5'] == 'on') {
        echo "任意字符重复";
        for($i=0;$i<(count($chararr)-1);$i++){

        }
    }

    $result = implode("\n", $charfinal);
    echo "<pre>{$result}</pre>";

}
*/
?>